package com.integralPerformance.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.integralPerformance.common.Result;
import com.integralPerformance.mapper.ApplicationManagerMapper;
import com.integralPerformance.pojo.entity.ApplicationManager;
import com.integralPerformance.pojo.entity.PositionElement;
import com.integralPerformance.pojo.form.add.AddApplicationManager;
import com.integralPerformance.pojo.form.update.UpdateApplicationManager;
import com.integralPerformance.pojo.query.ApplicationManagerQuery;
import com.integralPerformance.pojo.vo.ApplicationManagerVo;
import com.integralPerformance.pojo.vo.BaseVo;
import com.integralPerformance.service.ApplicationManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author admin
 * @description 针对表【application_manager(岗位申请管理表)】的数据库操作Service实现
 * @createDate 2024-04-03 21:42:22
 */
@Service
public class ApplicationManagerServiceImpl extends ServiceImpl<ApplicationManagerMapper, ApplicationManager>
        implements ApplicationManagerService {

    @Autowired
    ApplicationManagerMapper applicationManagerMapper;

    @Override
    public Result<BaseVo<ApplicationManagerVo>> getList(ApplicationManagerQuery applicationManagerQuery) {

        Page<ApplicationManagerVo> page = new Page<>(applicationManagerQuery.getCurrentPage(), applicationManagerQuery.getPageSize());
        Page<ApplicationManagerVo> result = applicationManagerMapper.getList(page, applicationManagerQuery);

        //json 转成 List<PositionElement> 并且重新塞进去
        result.getRecords().forEach(
                (applicationManagerVo) -> {
                    Object poc = applicationManagerVo.getPoc();
                    String string = (poc != null && !poc.toString().isEmpty()) ? poc.toString() : "[]";
                    List<PositionElement> positionElements = JSONArray.parseArray(string, PositionElement.class);
                    applicationManagerVo.setPoc(positionElements);
                    System.out.println(positionElements);
                }
        );
        BaseVo<ApplicationManagerVo> criteriaBaseVo = new BaseVo<>();
        criteriaBaseVo.setCurrentPage(applicationManagerQuery.getCurrentPage());
        criteriaBaseVo.setPageSize(applicationManagerQuery.getPageSize());
        criteriaBaseVo.setAllPages(result.getPages());
        criteriaBaseVo.setAllRecordNum(result.getTotal());
        criteriaBaseVo.setRecords(result.getRecords());

        return Result.success(criteriaBaseVo);
    }

    @Override
    public Result<String> addOne(AddApplicationManager addApplicationManager) {
        ApplicationManager applicationManager = new ApplicationManager();
        BeanUtil.copyProperties(addApplicationManager, applicationManager);
        List<PositionElement> poc = addApplicationManager.getPoc();
        String jsonString;
        if (poc != null) {
            jsonString = JSON.toJSONString(poc);
        } else {
            jsonString = "[]";
        }
        applicationManager.setPoc(jsonString);

        applicationManagerMapper.insert(applicationManager);
        return Result.success("添加成功");
    }

    @Override
    public Result<String> updateOne(UpdateApplicationManager updateApplicationManager) {
        ApplicationManager applicationManager = new ApplicationManager();
        BeanUtil.copyProperties(updateApplicationManager, applicationManager);
        List<PositionElement> poc = updateApplicationManager.getPoc();
        String jsonString;
        if (poc != null) {
            jsonString = JSON.toJSONString(poc);
        } else {
            jsonString = "[]";
        }
        applicationManager.setPoc(jsonString);
        applicationManagerMapper.updateById(applicationManager);
        return Result.success("修改成功");
    }

    @Transactional
    @Override
    public Result<String> deleteByIds(List<Integer> list) {
        applicationManagerMapper.deleteBatchIds(list);
        return Result.success("删除成功");
    }

    @Transactional
    @Override
    public Result<String> closeByIds(List<Integer> list) {
        applicationManagerMapper.closeByIds(list);
        return Result.success("修改成功");
    }

    @Transactional
    @Override
    public Result<String> openByIds(List<Integer> list) {
        applicationManagerMapper.openByIds(list);
        return Result.success("修改成功");
    }

}




